package com.example.shengyin.frontController;


import com.alibaba.fastjson2.JSON;
import com.example.shengyin.entity.Appoint;
import com.example.shengyin.entity.User;
import com.example.shengyin.service.AppointService;
import com.example.shengyin.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping("front/appoint")
@Validated
@CrossOrigin(origins = "*", maxAge = 3600,allowCredentials="true")
//swagger
@Api(tags = "AppointFrontController", description = "前端预约")
public class AppointFrontController {
    @Autowired
    private AppointService service;
    @Autowired
    private RedisTemplate redisTemplate;
    @ApiOperation("增加预约列表")
    @PostMapping("/add")
    public Result insert(@Valid  Appoint appoint, HttpServletRequest request)
    {
        String authorization = request.getHeader("Authorization");
        if (authorization==null) return Result.error("请先登录");
        User user = (User) redisTemplate.opsForValue().get(authorization);
        if (user==null) return Result.error("请先登录");
         System.out.println("out"+ JSON.toJSONString(appoint));
        appoint.setUserid((user.getId()));
        int insert = service.add(appoint);
        if(insert==0)
            return Result.error("添加失败");
        return Result.success("添加成功");
    }
}
